package cn.sstech.IM.webserver.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sstech.IM.constant.Constants;
import cn.sstech.IM.util.result.IBaseResult;
import cn.sstech.IM.webserver.user.model.Group;
import cn.sstech.IM.webserver.user.model.NoticeEntity;
import cn.sstech.IM.webserver.user.model.UserAccountEntity;
import cn.sstech.IM.webserver.user.model.UserFriend;
import cn.sstech.IM.webserver.user.service.UserChatService;
import io.swagger.annotations.Api;

@Controller
@RequestMapping("userchat")
@Api(description="用户聊天端相关接口")
public class UserChatController {
	
	@Autowired
	@Qualifier("userChatServiceImpl")
	UserChatService userChatService;
	
//	/**
//	 * 添加好友
//	 * @param userFriend
//	 * @return
//	 */
//	@RequestMapping(value="/friend", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult addFriend(@RequestBody UserFriend userFriend
//			,HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		userFriend.setFriendId(currentUser.getUserInfo().getId());
//		IBaseResult result = userChatService.addFriend(userFriend);
//
//		return result;
//	}
//	
//	/**
//	 * 删除好友
//	 * @param userFriend
//	 * @return
//	 */
//	@RequestMapping(value="/friend/{friendUserId}", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult delFriend(@PathVariable(value="friendUserId") Long friendUserId, HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		UserFriend userFriend = new UserFriend();
//		userFriend.setFriendId(friendUserId);
//		userFriend.setFriendId(currentUser.getUserInfo().getId());
//		IBaseResult result = userChatService.delFriend(userFriend);
//
//		return result;
//	}
	
	/**
	 * 创建群--默认将创建者置为群成员和管理员
	 * @return
	 */
	@RequestMapping(value="/group", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult addUserGroup(@RequestBody Group goup,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.addUserGroup(goup,currentUser.getUserInfo().getId());
		
		return result;
	}
	
//	/**
//	 * 删除群--只能是管理员删除;删除所有群成员
//	 * @param goup
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping(value="/group", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult delUserGroup(@RequestBody Group goup,HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		IBaseResult result = userChatService.delUserGroup(goup,currentUser.getUserInfo().getId());
//		
//		return result;
//	}
	
	/**
	 * 查找当前用户所在的群 和 群成员
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/group", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult getUserGroup(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.getUserGroup(currentUser.getUserInfo().getId());
		
		return result;
	}
	
//	/**
//	 * 加群加成员--只能是群的管理员加
//	 * @param goup
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping(value="/group/member", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult addGroupMember(@RequestBody Group goup,HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		IBaseResult result = userChatService.addGroupMember(goup,currentUser.getUserInfo().getId());
//		
//		return result;
//	}
//	
//	@RequestMapping(value="/group/member", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult delGroupMember(@RequestBody Group goup,HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		IBaseResult result = userChatService.delGroupMember(goup,currentUser.getUserInfo().getId());
//		
//		return result;
//	}
	
//	/**
//	 * 拉取离线消息
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping(value="/group/offlineMsg", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
//	@ResponseBody
//	public IBaseResult pullOfflineMsg(HttpServletRequest request){
//		HttpSession session = request.getSession();
//		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
//		IBaseResult result = userChatService.pullOfflineMsg(currentUser.getUserInfo().getId());
//		
//		return result;
//	}
	
	/**
	 * 添加 好友 或 群
	 * @param noticeEntity
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/notice", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult noticeAdd(@RequestBody NoticeEntity noticeEntity,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.noticeAdd(noticeEntity,currentUser.getUserInfo().getId(),currentUser.getId().toString());
		
		
		return result;
	}
	
	/**
	 * 设置消息为已读
	 */
	@RequestMapping(value="/notice/hasRead/{noticeId}", method=RequestMethod.PUT, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult noticeReaded(@PathVariable("noticeId") Long noticeId ,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.noticeReaded(noticeId,currentUser.getUserInfo().getId());
		
		
		return result;
	}
	
	/**
	 * 删除好友
	 */
	@RequestMapping(value="/friend", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult delFriend(@RequestBody NoticeEntity noticeEntity,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.delFriend(noticeEntity,currentUser.getUserInfo().getId());
		
		
		return result;
	}
	
	/**
	 * 删除群成员
	 */
	@RequestMapping(value="/group/member", method=RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public IBaseResult delGroupMember(@RequestBody NoticeEntity noticeEntity,HttpServletRequest request){
		HttpSession session = request.getSession();
		UserAccountEntity currentUser = (UserAccountEntity) session.getAttribute(Constants.SessionAttributeName.USER);
		IBaseResult result = userChatService.delGroupMember(noticeEntity,currentUser.getUserInfo().getId());
		
		
		return result;
	}
}
